﻿using System;
using System.Net;
using System.Windows;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MiruPublic;

namespace MangaKurosagi
{
    public class LeadBlogtruyen:Website
    {
        private List<ItemDetails> _ListStories;
        public List<ItemDetails> ListStories
        {
            get { return _ListStories; }
            set 
            { 
                _ListStories = value;
            }
        }
        public bool leadAll=false;

        public event EventHandler<LeadListCompletedEventArgs> ListUpdateSucess;
        protected virtual void OnListUpdateSucess(LeadListCompletedEventArgs e)
        {
            EventHandler<LeadListCompletedEventArgs> handler = ListUpdateSucess;
            if (handler != null)
            {
                handler(this, e);
            }
        }
        public event EventHandler<LeadListAllCompletedEventArgs> ListAllStoriesUpdateSucess;
        protected virtual void OnListAllStoriesUpdateSucess(LeadListAllCompletedEventArgs e)
        {
            EventHandler<LeadListAllCompletedEventArgs> handler = ListAllStoriesUpdateSucess;
            if (handler != null)
            {
                handler(this, e);
            }
        }
        protected override void OnGetContentSucess(DownloadStringCompletedEventArgs e)
        {
            base.OnGetContentSucess(e);

            if (!leadAll)
            {
                ListStories = GetList(e.Result);

                LeadListCompletedEventArgs args = new LeadListCompletedEventArgs();
                args.listResult = ListStories;
                args.PageCount = GetPageCount(Utils.GetStringBetweenText(e.Result, "<div class=\"paging-div\">", "</div>"));
                OnListUpdateSucess(args);
            }
            else
            {
                LeadListAllCompletedEventArgs args = new LeadListAllCompletedEventArgs();
                args.listResult = GetListAll(Utils.GetStringBetweenText(e.Result,"<div class=\"danhsach\">","<div class=\"paging-div\""));
                args.PageCount = GetPageCount(Utils.GetStringBetweenText(e.Result, "<div class=\"paging-div\"", "</div>"));
                OnListAllStoriesUpdateSucess(args);
            }
        }
        private int GetPageCount(string content)
        {
            string CheckContent = content;
            string line = "";
            line = Utils.GetStringBetweenText(CheckContent, "<a href=", "/a>");
            while (line != "")
            {
                if (line.IndexOf("<span>Last</span>") >= 0)
                {
                    return int.Parse(Utils.GetStringBetweenText(line, leadAll == false ? "/trangchu/?page=" : "/danhsach/?page=", "\""));
                }
                
                CheckContent = Utils.GetStringBetweenText(CheckContent,line, "");

                line = Utils.GetStringBetweenText(CheckContent, "<a href=", "/a>");
            }
            return 1;
        }
        private List<ItemDetails> GetList(string content)
        {
            List<ItemDetails> Result = new List<ItemDetails>();
            string CheckContent = content;
            string line = "";
            line = Utils.GetStringBetweenText(CheckContent, "<a href=", "/a>");
            while (line != "")
            {

                string Title = HttpUtility.HtmlDecode(Utils.GetStringBetweenText(line, "<h3>", "</h3>"));
                string Image = Utils.GetStringBetweenText(line, "<img src=\"", "\"").Trim();
                string Description = HttpUtility.HtmlDecode(Utils.GetStringBetweenText(line, "<span class=\"soluoc\">", "</span>").Trim());
                ItemDetails item = new ItemDetails();
                item.Title = Title;
                if (Title != "" && Image != "")
                {
                    item.Thumbnail.Source = new BitmapImage(new Uri(Image));
                    item.Description = Description;
                    item.UrlImage = Image;
                    item.Url = "http://m.blogtruyen.com" + Utils.GetStringBetweenText(line, "\"", "\"");
                    Result.Add(item);
                }
                CheckContent = Utils.GetStringBetweenText(CheckContent, "<div class=\"clear\">", "");

                line = Utils.GetStringBetweenText(CheckContent, "<a href=", "/a>");
            }
            return Result;
        }

        private List<Chap> GetListAll(string content)
        {
            List<Chap> Result = new List<Chap>();
            string CheckContent = content;
            string line = "";
            line = Utils.GetStringBetweenText(CheckContent, "<a href=", "/a>");
            while (line != "")
            {
                string title = HttpUtility.HtmlDecode(Utils.GetStringBetweenText(line, "title=\"", "\""));

                if(title!="")
                {
                    Chap item = new Chap();
                    item.Title = title;
                    item.Url = "http://m.blogtruyen.com" + Utils.GetStringBetweenText(line, "\"", "\"");
                    Result.Add(item);
                }   
                CheckContent = Utils.GetStringBetweenText(CheckContent,line, "");

                line = Utils.GetStringBetweenText(CheckContent, "<a href=", "/a>");
            }
            return Result;
        }
    }
    public class LeadListCompletedEventArgs : EventArgs
    {
        public List<ItemDetails> listResult;
        public int PageCount;
    }
    public class LeadListAllCompletedEventArgs:EventArgs
    {
        public List<Chap> listResult;
        public int PageCount;
    }
}
